#include <stdio.h>
#include <stdlib.h>

#include <semaphore.h>
#include <pthread.h>

sem_t sem1, sem2;

void *th_a(void *arg)
{
    sem_wait(&sem1);
    printf("Thread A running!\n");
    return (void *)0;
}

void *th_b(void *arg)
{
    sem_wait(&sem2);
    printf("Thread B running!\n");
    sem_post(&sem1);        // 释放线程 A，让线程A继续执行
    return (void *)0;
}

void *th_c(void *arg)
{
    printf("Thread C running!\n");
    sem_post(&sem2);    // 释放线程 B， 让线程B继续执行
    return (void *)0;
}

int main(void)
{
    pthread_t tha, thb, thc;

    sem_init(&sem1, 0, 0);
    sem_init(&sem2, 0, 0);

    pthread_create(&tha, NULL, th_a, NULL);
    pthread_create(&thb, NULL, th_b, NULL);
    pthread_create(&thc, NULL, th_c, NULL);

    pthread_join(tha,NULL);
    

    sem_destroy(&sem1);
    sem_destroy(&sem2);

    return 0;
}